package com.example.chaomi;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

public class FG extends Fragment {
	public FG() {
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
		View view = inflater.inflate(R.layout.fg, null);
		final ListView  lv = (ListView) view.findViewById(R.id.fg_lv);
		lv.setAdapter(new TitleAdapter(getActivity()));
		lv.requestFocus();
		lv.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//lv.getChildAt(2).setSelected(true);
				lv.setSelection(MainActivity.mPos);;
			}
		});
//		lv.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//				if(arg2 == 0) {
//					MainActivity.mPos = 3;
//					Fragment fragment2 = getFragmentManager().findFragmentById(R.id.fg);
//					FragmentTransaction ft = getFragmentManager().beginTransaction();
//					fragment2 = new FG2();
//					ft.replace(R.id.fg, fragment2);
//					ft.commit();					
//				}
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				
//			}
//		});
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2 == 1) {
					MainActivity.mPos = 2;
					Fragment fragment2 = getFragmentManager().findFragmentById(R.id.fg);
					FragmentTransaction ft = getFragmentManager().beginTransaction();
					fragment2 = new FG2();
					ft.replace(R.id.fg, fragment2);
					ft.commit();					
				}
			}
		});
		return view;
		
	}

}
